import api from '../../../common/api.js'
import {
  insertHouseLog
} from '../../../common/log.js';
import constant from '../../../common/constant.js';
import {
  _debounce
} from './../../../utils/utilsNew';
import * as transferData from '../../../models/transferData'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSearchOnce: false,
    city: {},
    rows: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData(options)
    this.uploadLog()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.myData.entryPageCount++
    this.refreshCity()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  initData(options) {
    let city = wx.getStorageSync('currentCity') || {}
    this.myData = {
      keyWords: '',
      entryPageCount: 0
    }
    this.setData({
      city
    })
  },

  uploadLog() {
    const {
      city
    } = this.data
    insertHouseLog({
      logType: "2",
      cityName: city.city || "上海市",
      inputHouseType: app.getCurrentStatusName()
    })
  },

  onSwitchCity() {
    wx.navigateTo({
      url: '/pages/home/switchCity/switchCity',
    })
  },

  onSearchCancel() {
    wx.navigateBack()
  },

  onAddHouse(e) {
    const {
      keyWords
    } = this.myData
    let villageData = e.currentTarget.dataset.village
    wx.navigateTo({
      url: `/pages/cooperation/houseEdit/houseEdit?villageData=${JSON.stringify(villageData)}`
    })
    wx.gio('track', constant.CLICKS_SEARCH_RESULT, {
      [constant.SEARCHFROM_VAR]: constant.INPUT_SEARCH,
      [constant.SEARCHWORD_VAR]: keyWords || '',
      [constant.SEARCHSOURCE_VAR]: constant.XIAOQU_PAGE_SEARCH,
    })
  },

  onInputChange: _debounce(function (e) {
    let keyWords = e.detail.value || ''
    if (!(keyWords && keyWords.trim())) {
      this.myData.keyWords = ''
      this.setData({
        rows: [],
      })
      return
    }
    const {
      city
    } = this.data
    this.myData.keyWords = keyWords

    api.listForCooperation({
      resblockName: keyWords,
      pageNo: 1,
      rows: 10,
      city: city ? city.city : '',
      isExternal: null
    }, (res) => {
      if (res && res.data && res.data.isSuccess && res.data.content) {
        const {
          rows = []
        } = res.data.content || {}
        this.setData({
          isSearchOnce: true,
          rows: rows || [],
        })
        // 日志
        insertHouseLog({
          logType: "3",
          cityName: city ? city.city : '',
          inputHouseType: app.getCurrentStatusName(),
          searchContent: keyWords,
          resultNum: rows.length
        })
        // 日志
        wx.gio('track', constant.SEARCH_SUCCESS, {
          [constant.SEARCHFROM_VAR]: constant.INPUT_SEARCH,
          [constant.IFSEARCHRESULT_VAR]: !!rows.length,
          [constant.SEARCHWORD_VAR]: keyWords,
          [constant.SEARCHSOURCE_VAR]: constant.XIAOQU_PAGE_SEARCH,
        })
      } else {
        this.setData({
          loading: false,
        })
      }
    }, () => {
      this.setData({
        loading: false,
      })
    })

  }, 500),

  // 检察城市更新
  refreshCity() {
    if (transferData.getData('currentCity')) {
      transferData.resetData('currentCity')
    }
    if (this.myData.entryPageCount > 1) {
      const {
        city
      } = this.data
      let currentCity = wx.getStorageSync('currentCity') || {}
      if (city && city.cityCode && currentCity && currentCity.cityCode && city.cityCode !== currentCity.cityCode) {
        this.setData({
          rows: [],
          city: {
            city: currentCity.city,
            cityCode: currentCity.cityCode
          },
        })
        this.onInputChange({
          detail: {
            value: this.myData.keyWords || ''
          }
        })
      }
    }
  },


})